<?php
namespace Admin\Controller;

class CreateController extends HomeController {
    protected $html_name;

    //生成页面首页
    public function create_page(){
        $this->classifys=M("createpage")->select();
        //这行代码删除生成的页面
//        unlink('Application/Admin/View/create/article.html');
        $this->page_title="ھاسىل بولغان بەتلەر";
        $this->display("index");
    }
    function index(){
        $i=M("createpage")->count();
        $this->html_name="create".($i+1);
        $this->new_html();
    }
    //这行代码删除生成的页面
    public function del(){
        if(IS_AJAX){
            $post=I('post.');
            $row=unlink($post['link']);
            if($row){
                M("createpage")->delete($post['id']);
                echo response_jison_exit('1',"ئۆچۈرۈلدى",$post['id']);
            }else{
                echo response_jison_exit('0',"مەغلۇب بولدى");
            }
        }
    }
    //بەت ھاسىل قىلىش
    public function new_html(){
        $post=I('post.');
        //将数据存入二维数组
        $con = array(
            1=>html_entity_decode($post['title']),
            2=>date("Y-m-d H:i"),
            3=>html_entity_decode($post['content']),
            5=>$post['my1_0'],
            6=>$post['my1_1'],   //tel
            7=>$post['my2_0'],
            8=>$post['my2_1'],//QQ
            9=>$post['my3_0'],//QQ
           10=>$post['my3_1'],//QQ
            11=>$post['my4_0'],
            12=>$post['my4_1'],//QQ
            13=>$post['my5_0'],//QQ
            14=>$post['my5_1'],//QQ
            //color
            15=>$post['bgcolor'],//QQ
            16=>$post['color'],//QQ
        );
        $title = $con[0];
        $tema = $con[1];
        $time = $con[2];
        $content = $con[3];
        $my1_0 = $con[5]?$con[5]:' ';
        $my1_1 = $con[6]?$con[6]:' ';
        $my2_0 = $con[7]?$con[7]:' ';
        $my2_1 = $con[8]?$con[8]:' ';
        $my3_0 = $con[9]?$con[9]:' ';
        $my3_1 = $con[10]?$con[10]:' ';
        $my4_0 = $con[11]?$con[11]:' ';
        $my4_1 = $con[12]?$con[12]:' ';
        $my5_0 = $con[13]?$con[13]:' ';
        $my5_1 = $con[14]?$con[14]:' ';
        $bottom_color=$con[5]||$con[6]||$con[7]||$con[8]||$con[9]||$con[10]?"alert alert-danger":"";
        $bg_color=$con[15]?$con[15]:'#fff';
        $color=$con[16]?$con[16]:'#444';
        $path = "Application/Admin/View/create/".$this->html_name.".html";
        //替换example内容，并获取内容赋值给$str
        $fp = fopen("example.html", "r");
        $str = fread($fp, filesize("example.html"));
        //replace
        $str = str_replace("{title}", $title, $str);
        $str = str_replace("{tema}", $tema, $str);
        $str = str_replace("{time}", $time, $str);
        $str = str_replace("{content}", $content, $str);
        $str = str_replace("{my1_0}", $my1_0, $str);
        $str = str_replace("{my1_1}", $my1_1, $str);
        $str = str_replace("{my2_0}", $my2_0, $str);
        $str = str_replace("{my2_1}", $my2_1, $str);
        $str = str_replace("{my3_0}", $my3_0, $str);
        $str = str_replace("{my3_1}", $my3_1, $str);
        $str = str_replace("{my4_0}", $my4_0, $str);
        $str = str_replace("{my4_1}", $my4_1, $str);
        $str = str_replace("{my5_0}", $my5_0, $str);
        $str = str_replace("{my5_1}", $my5_1, $str);
        //color
        $str = str_replace("{color_bottom}", $bottom_color, $str);
        $str = str_replace("{bg_color}", $bg_color, $str);
        $str = str_replace("{color}", $color, $str);
        fclose($fp);

        //新建空白文件，将$str写入
        $handle = fopen($path, "w");
        fwrite($handle, $str);
        fclose($handle);
        $data['htmlname']=$path;
        $data['intime']=time();
        M("createpage")->add($data);
        $this->display("../index");
        _check_alert("بەت ھاسىل بولۇندى !","create_page.html",1);
    }

}